package com.feihao.zhou.ThreadSaveDemo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket extends Thread {
    private static int ticket = 100;

    private static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();
            if (ticket<=0){
                break;
            }else{
                try {
                    ticket--;
                    Thread.sleep(50);
                    System.out.println(Thread.currentThread().getName()+
                            "正在卖票，还有" + ticket+"张票");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }
}
